package GUI.TabPanels.AdminTabs;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableRowSorter;

import comparators.IntegerComparator;

import GUI.Controller.Controller;

@SuppressWarnings("serial")
public class CreateAccountPanel extends JPanel{
	private JLabel customer_firstname_label;
	private JTextField customer_firstname_text;

	private JLabel customer_lastname_label;
	private JTextField customer_lastname_text;

	private JLabel customer_email_label;
	private JTextField customer_email_text;

	private JLabel customer_password_label;
	private JPasswordField customer_password_text;

	private JLabel customer_address_label;
	private JTextField customer_address_text;

	private JLabel customer_phno_label;
	private JTextField customer_phno_text;
	
	private JLabel credit_card_label;
	private JTextField credit_card_text;
	
	private JLabel account_types_label;
	private JComboBox account_types_list;
	
	private JLabel customer_subscription_label;
	private JCheckBox customer_subscription_checkbox ;
	
	private JButton add_account_button;

	private JTable account_table;

	private JPanel topPanel;
	private JPanel middlePanel;
	private JScrollPane scrollPane;

	private String[] columnNames =
	{
			"Email",
			"Password",
			"Phone Number",
			"Type"
	};

	public CreateAccountPanel(){
		this.setBackground(Color.PINK);
		this.setLayout(new BorderLayout());
		this.SetUpTopPanel();
		this.SetUpMiddlePanel();
		this.SetUpBottomPanel();
	}

	public void SetUpTopPanel(){
		topPanel = new JPanel();
		this.add(topPanel, BorderLayout.NORTH);
		topPanel.setLayout(new GridLayout(0, 2, -300, 7));
		topPanel.setBackground(Color.pink);
		
		customer_email_label = new JLabel("Email");
		customer_email_text = new JTextField(9);
		customer_email_text.setEditable(true);
		topPanel.add(customer_email_label);
		topPanel.add(customer_email_text);
		
		customer_password_label = new JLabel("Password");
		customer_password_text = new JPasswordField(9);
		customer_password_text.setEditable(true);
		topPanel.add(customer_password_label);
		topPanel.add(customer_password_text);

		customer_firstname_label = new JLabel("First Name:");
		customer_firstname_text = new JTextField(9);
		customer_firstname_text.setEditable(true);
		topPanel.add(customer_firstname_label);
		topPanel.add(customer_firstname_text);
		
		customer_lastname_label = new JLabel("Last Name:");
		customer_lastname_text = new JTextField(9);
		customer_lastname_text.setEditable(true);
		topPanel.add(customer_lastname_label);
		topPanel.add(customer_lastname_text);
		
		customer_address_label = new JLabel("Address:");
		customer_address_text = new JTextField(9);
		customer_address_text.setEditable(true);
		topPanel.add(customer_address_label);
		topPanel.add(customer_address_text);
		
		customer_phno_label = new JLabel("Phone Number:");
		customer_phno_text = new JTextField(9);
		customer_phno_text.setEditable(true);
		topPanel.add(customer_phno_label);
		topPanel.add(customer_phno_text);
		
		credit_card_label = new JLabel("Credit Card Number");
		credit_card_text = new JTextField(9);
		credit_card_text.setEditable(true);
		topPanel.add(credit_card_label);
		topPanel.add(credit_card_text);
		
		account_types_label = new JLabel("Account type:");
		String[] types = {"Customer", "Employee", "Admin"};
		account_types_list = new JComboBox(types);
		topPanel.add(account_types_label);
		topPanel.add(account_types_list);
		
		customer_subscription_label = new JLabel("Do you want to subscribe to special offers?");
		customer_subscription_checkbox = new JCheckBox();
		topPanel.add(customer_subscription_label);
		topPanel.add(customer_subscription_checkbox);
	}

	public void SetUpMiddlePanel(){
		JLabel emptyLabel = new JLabel();
		middlePanel = new JPanel();
		middlePanel.setBackground(Color.pink);
		
		add_account_button = new JButton("Add Account");
		add_account_button.addActionListener(new AddNewAccountButton());

		middlePanel.add(emptyLabel);
		middlePanel.add(add_account_button);
		middlePanel.add(emptyLabel);

		this.add(middlePanel, BorderLayout.CENTER);
	}

	public void SetUpBottomPanel(){
		Object[][] tableData = Controller.getAccounts();;

		CreateTable(tableData);
	}

	private void Update()
	{

		Object[][] tableData = Controller.getAccounts();

		CreateTable(tableData);

		account_table.updateUI();
		
		this.setVisible(false);
		this.setVisible(true);
	}

	private void CreateTable(Object[][] tableData){
		account_table = new JTable(tableData, columnNames){
			@Override 
			public boolean isCellEditable(int row, int column) { 
				return false;
			} 
		};
		account_table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

		DefaultTableModel model = new DefaultTableModel(tableData, columnNames);

		TableRowSorter<DefaultTableModel> sorter = new TableRowSorter<DefaultTableModel>(model);

		sorter.setComparator(0, new IntegerComparator());
		sorter.setComparator(1, new IntegerComparator());

		account_table.setRowSorter(sorter);
		account_table.setAutoCreateRowSorter(false);

		account_table.setPreferredScrollableViewportSize(new Dimension(500, 150));
		account_table.setFillsViewportHeight(true);

		if(scrollPane!=null)
			remove(scrollPane);
		scrollPane = new JScrollPane(account_table);
		this.add(scrollPane, BorderLayout.SOUTH);
	}

	private class AddNewAccountButton implements ActionListener{

		@SuppressWarnings("deprecation")
		@Override
		public void actionPerformed(ActionEvent arg0) {
			try
			{
				Long.parseLong(customer_phno_text.getText());
				Integer.parseInt(credit_card_text.getText());
				if (customer_lastname_text.getText().compareTo("") == 0 
						|| customer_lastname_text.getText().length() < 2
						|| customer_firstname_text.getText().compareTo("") == 0 
						|| customer_firstname_text.getText().length() < 2
						|| customer_password_text.getText().compareTo("") == 0 
						|| customer_password_text.getText().length() < 6
						|| customer_email_text.getText().compareTo("") == 0 
						|| customer_email_text.getText().length() < 2
						|| customer_address_text.getText().compareTo("") == 0 
						|| customer_address_text.getText().length() < 2
						|| customer_phno_text.getText().compareTo("") == 0 
						|| customer_phno_text.getText().length() < 2)
					throw new Exception();
				
				// String[] customerParams = { "LastName", "FirstName", "PsWd", "email",
				// "0", "address","4038888888", "0" };
				String[] data = {
						customer_lastname_text.getText(),
						customer_firstname_text.getText(),
						customer_password_text.getText(),
						customer_email_text.getText(),
						Integer.toString(account_types_list.getSelectedIndex()),
						customer_address_text.getText(),
						customer_phno_text.getText(),
						(customer_subscription_checkbox.isSelected() ? "0" : "1")
				};

				Controller.CreateCustomer(data);
				JOptionPane.showMessageDialog(null, "Account Created!");
			}
			catch(NumberFormatException e)
			{
				JOptionPane.showMessageDialog(null, "Phone Number and Credit Card must be integers.");
			}
			catch(Exception e)
			{
				JOptionPane.showMessageDialog(null, "Fields cannot be empty or less than 2 characters. Passwords must be at least 6 characters long.");
			}
			Update();
		}
	}
}
